#include <stdio.h>
#include <assert.h>

#include "bitmatrix.h"

static void set_matrix(bit_matrix * m) {
	unsigned r, c;
	
	for(r = 0; r < get_num_rows(m); ++r) {
		for(c = 0; c < get_num_cols(m); ++c) {	
			set_bit(m, r, c);
		}
	}
}

static void unset_matrix(bit_matrix * m) {
	unsigned r, c;
	
	for(r = 0; r < get_num_rows(m); ++r) {
		for(c = 0; c < get_num_cols(m); ++c) {	
			unset_bit(m, r, c);
		}
	}
}

static void set_row(bit_matrix * m, unsigned row) {
	unsigned i;
	for(i = 0; i < get_num_cols(m); ++i) {
		set_bit(m, row, i);
	}
}

static void set_col(bit_matrix * m, unsigned col) {
	unsigned i;
	for(i = 0; i < get_num_rows(m); ++i) {
		set_bit(m, i, col);
	}
}

static void set_diag(bit_matrix * m) {
	unsigned i = 0;
	while(i < get_num_rows(m) && i < get_num_cols(m)) {
		set_bit(m, i, i);
		i += 1;
	}
}

int main (int argc, char const *argv[]) {
	
	printf("--\n");
	bit_matrix * m = allocate_matrix(23, 34);
	print_matrix(m);
	
	printf("--\n");
	set_matrix(m);
	print_matrix(m);

	printf("--\n");	
	unset_matrix(m);
	print_matrix(m);

	printf("--\n");
	set_row(m, 0);
	set_row(m, 22);
	print_matrix(m);
		
	printf("--\n");
	set_col(m, 0);
	set_col(m, 33);
	print_matrix(m);
		
	printf("--\n");
	set_diag(m);
	print_matrix(m);

	printf("--\n");
	unsigned r, c;
	for(r = 0; r < get_num_rows(m); ++r) {
		for(c = 0; c < get_num_cols(m); ++c) {
			
			if(r == 0 || r == (get_num_rows(m)-1)) {
				//First and last rows all set to 1
				assert(is_bit_set(m, r, c));
			} else if(c == 0 || c == (get_num_cols(m)-1)) {
				//First and last cols all set to 1
				assert(is_bit_set(m, r, c));
			} else if (r == c) {
				//Diagonal from 0,0 to n, n all set to 1
				assert(is_bit_set(m, r, c));
			} else {
				//All the rest is unset
				assert(is_bit_set(m, r, c) == false);
			}
		}
	}
	destroy_matrix(m);
	
	printf("--\n");
	bool result;
	FILE * f = fopen("testfiles/test.parsematrix.txt", "r");
	assert(f != NULL);

	m = allocate_matrix(8, 5);
	result = matrix_fill_from_file(m, f);
	assert(result == true);
	print_matrix(m);
	
	fclose(f);	

	printf("TEST OK\n");
	return 0;
	
	//Shut up gcc...
	argc = argc;
	argv = argv;
}